package com.op.crm.util;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.struts2.ServletActionContext;

import com.op.crm.persist.model.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public abstract class BaseAction<T> extends ActionSupport implements Preparable,ModelDriven<T>{

	private static final long serialVersionUID = 1L;
	
	//session名称
	protected static final String CRM_USER = "crm_user";
	
	protected List<String> returnValues = new ArrayList<String>();
	protected String returnValue = null;


	protected String mapping = ERROR;

//    protected int  currentPage = 1;

//    protected Pager pager = new Pager(this.currentPage);
	
	HttpServletRequest request;
	
	HttpServletResponse response;

	public static long getSerialversionuid() {
		return serialVersionUID;
	}


	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}


	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}
    

	public String getMapping() {
		return mapping;
	}


	public void setMapping(String mapping) {
		this.mapping = mapping;
	}


	protected Date getCurrentDate() {
		return new Date();
	}


	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	public ServletContext getServletContext() {
		return ServletActionContext.getServletContext();
	}

	public String getRealyPath(String path) {
		return getServletContext().getRealPath(path);
	}

	public void prepare() throws Exception {

	}

	public Map<String ,Object> getSession() {
		return ActionContext.getContext().getSession();
	}


	public List<String> getReturnValues() {
		return returnValues;
	}


	public void setReturnValues(List<String> returnValues) {
		this.returnValues = returnValues;
	}


	public String getReturnValue() {
		return returnValue;
	}


	public void setReturnValue(String returnValue) {
		this.returnValue = returnValue;
	}
	
	public void addReturnValue(String returnValue) {
		this.returnValues.add(returnValue);
	}
	
	public User getCurrentCRMUser() {
		return (User)getSession().get(CRM_USER);
	}
	
	public void writeObject(Object obj) {
		try {
			JSONObject jsonObj = JSONObject.fromObject(obj); 
			this.getResponse().setContentType("application/json;charset=utf-8");
			PrintWriter out = getResponse().getWriter();
			out.write(jsonObj.toString());
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void writeObject(Object obj,String[] excludes) {
		try {
			if (excludes != null && excludes.length > 0) {
				JsonConfig jconfig = new JsonConfig();
				jconfig.setIgnoreDefaultExcludes(false);
				jconfig.setExcludes(excludes);
				JSONObject jsonObj = JSONObject.fromObject(obj,jconfig); 
				this.getResponse().setContentType("application/json;charset=utf-8");
				PrintWriter out = getResponse().getWriter();
				out.write(jsonObj.toString());
				out.flush();
			} else {
				writeObject(obj);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	
}
